﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using HslCommunication.BasicFramework;
using HslCommunication.Core;
using HslCommunication.Reflection;
using HslCommunication.Serial;

namespace HslCommunication.Profinet.LSIS
{
	/// <summary>
	/// XGk Cnet I/F module supports Serial Port.
	/// </summary>
	/// <remarks>
	/// </remarks>
	// Token: 0x02000085 RID: 133
	public class XGKCnet : SerialDeviceBase
	{
		/// <summary>
		/// Instantiate a Default object
		/// </summary>
		// Token: 0x06000A3C RID: 2620 RVA: 0x0003CFC2 File Offset: 0x0003B1C2
		public XGKCnet()
		{
			base.ByteTransform = new RegularByteTransform();
			base.WordLength = 2;
		}

		/// <inheritdoc cref="P:HslCommunication.Profinet.LSIS.XGBCnetOverTcp.Station" />
		// Token: 0x170001AD RID: 429
		// (get) Token: 0x06000A3D RID: 2621 RVA: 0x0003CFE7 File Offset: 0x0003B1E7
		// (set) Token: 0x06000A3E RID: 2622 RVA: 0x0003CFEF File Offset: 0x0003B1EF
		public byte Station { get; set; } = 5;

		/// <inheritdoc cref="M:HslCommunication.Profinet.LSIS.XGBCnetOverTcp.ReadByte(System.String)" />
		// Token: 0x06000A3F RID: 2623 RVA: 0x0003AB36 File Offset: 0x00038D36
		[HslMqttApi("ReadByte", "")]
		public OperateResult<byte> ReadByte(string address)
		{
			return ByteTransformHelper.GetResultFromArray<byte>(this.Read(address, 2));
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.LSIS.XGBCnetOverTcp.Write(System.String,System.Byte)" />
		// Token: 0x06000A40 RID: 2624 RVA: 0x0001D14A File Offset: 0x0001B34A
		[HslMqttApi("WriteByte", "")]
		public OperateResult Write(string address, byte value)
		{
			return this.Write(address, new byte[]
			{
				value
			});
		}

		/// <inheritdoc />
		// Token: 0x06000A41 RID: 2625 RVA: 0x0003CFF8 File Offset: 0x0003B1F8
		[HslMqttApi("ReadBoolArray", "")]
		public override OperateResult<bool[]> ReadBool(string address, ushort length)
		{
			List<XGTAddressData> list = new List<XGTAddressData>();
			string[] array = address.Split(new string[]
			{
				";",
				","
			}, StringSplitOptions.RemoveEmptyEntries);
			foreach (string text in array)
			{
				XGTAddressData xgtaddressData = new XGTAddressData();
				OperateResult<XGT_DataType, bool> dataTypeToAddress = XGKFastEnet.GetDataTypeToAddress(text);
				bool content = dataTypeToAddress.Content2;
				if (content)
				{
					xgtaddressData.Address = text.Substring(1);
				}
				else
				{
					xgtaddressData.Address = text.Substring(2);
				}
				list.Add(xgtaddressData);
			}
			OperateResult<XGT_MemoryType> operateResult = XGKFastEnet.AnalysisAddress(array[0]);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				OperateResult<XGT_DataType, bool> dataTypeToAddress2 = XGKFastEnet.GetDataTypeToAddress(array[0]);
				bool flag2 = !dataTypeToAddress2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<bool[]>(dataTypeToAddress2);
				}
				else
				{
					OperateResult<byte[]> operateResult2 = this.Read(dataTypeToAddress2.Content1, list, operateResult.Content, 1);
					OperateResult<byte[]> operateResult3 = base.ReadBase(operateResult2.Content);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = OperateResult.CreateFailedResult<bool[]>(operateResult3);
					}
					else
					{
						result = OperateResult.CreateSuccessResult<bool[]>(SoftBasic.ByteToBoolArray(XGKCnet.ExtractActualData(operateResult3.Content, true).Content, (int)length));
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.LSIS.XGBCnetOverTcp.ReadCoil(System.String)" />
		// Token: 0x06000A42 RID: 2626 RVA: 0x0003ABD2 File Offset: 0x00038DD2
		public OperateResult<bool> ReadCoil(string address)
		{
			return this.ReadBool(address);
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.LSIS.XGBCnetOverTcp.ReadCoil(System.String,System.UInt16)" />
		// Token: 0x06000A43 RID: 2627 RVA: 0x0003ABDB File Offset: 0x00038DDB
		public OperateResult<bool[]> ReadCoil(string address, ushort length)
		{
			return this.ReadBool(address, length);
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.LSIS.XGKCnet.WriteCoil(System.String,System.Boolean)" />
		// Token: 0x06000A44 RID: 2628 RVA: 0x0003ABE5 File Offset: 0x00038DE5
		public OperateResult WriteCoil(string address, bool value)
		{
			return this.Write(address, value);
		}

		/// <inheritdoc />
		// Token: 0x06000A45 RID: 2629 RVA: 0x0003ABEF File Offset: 0x00038DEF
		[HslMqttApi("WriteBool", "")]
		public override OperateResult Write(string address, bool value)
		{
			return this.Write(address, new byte[]
			{
				value ? 1 : 0
			});
		}

		/// <inheritdoc />
		// Token: 0x06000A46 RID: 2630 RVA: 0x0003D13C File Offset: 0x0003B33C
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, bool value)
		{
			XGKCnet.<WriteAsync>d__12 <WriteAsync>d__ = new XGKCnet.<WriteAsync>d__12();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<XGKCnet.<WriteAsync>d__12>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06000A47 RID: 2631 RVA: 0x0003D190 File Offset: 0x0003B390
		[HslMqttApi("ReadByteArray", "")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			List<XGTAddressData> list = new List<XGTAddressData>();
			string[] array = address.Split(new string[]
			{
				";",
				","
			}, StringSplitOptions.RemoveEmptyEntries);
			foreach (string text in array)
			{
				XGTAddressData xgtaddressData = new XGTAddressData();
				OperateResult<XGT_DataType, bool> dataTypeToAddress = XGKFastEnet.GetDataTypeToAddress(text);
				bool content = dataTypeToAddress.Content2;
				if (content)
				{
					xgtaddressData.Address = text.Substring(1);
				}
				else
				{
					xgtaddressData.Address = text.Substring(2);
				}
				list.Add(xgtaddressData);
			}
			OperateResult<XGT_MemoryType> operateResult = XGKFastEnet.AnalysisAddress(array[0]);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				OperateResult<XGT_DataType, bool> dataTypeToAddress2 = XGKFastEnet.GetDataTypeToAddress(array[0]);
				bool flag2 = !dataTypeToAddress2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<byte[]>(dataTypeToAddress2);
				}
				else
				{
					OperateResult<byte[]> operateResult2 = this.Read(dataTypeToAddress2.Content1, list, operateResult.Content, (int)length);
					bool flag3 = !operateResult2.IsSuccess;
					if (flag3)
					{
						result = operateResult2;
					}
					else
					{
						OperateResult<byte[]> operateResult3 = base.ReadBase(operateResult2.Content);
						bool flag4 = !operateResult3.IsSuccess;
						if (flag4)
						{
							result = operateResult3;
						}
						else
						{
							result = XGKCnet.ExtractActualData(operateResult3.Content, true);
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000A48 RID: 2632 RVA: 0x0003D2D8 File Offset: 0x0003B4D8
		[HslMqttApi("WriteByteArray", "")]
		public override OperateResult Write(string address, byte[] value)
		{
			List<XGTAddressData> list = new List<XGTAddressData>();
			string[] array = address.Split(new string[]
			{
				";",
				","
			}, StringSplitOptions.RemoveEmptyEntries);
			foreach (string text in array)
			{
				XGTAddressData xgtaddressData = new XGTAddressData();
				OperateResult<XGT_DataType, bool> dataTypeToAddress = XGKFastEnet.GetDataTypeToAddress(text);
				bool content = dataTypeToAddress.Content2;
				if (content)
				{
					xgtaddressData.Address = text.Substring(1);
				}
				else
				{
					xgtaddressData.Address = text.Substring(2);
				}
				xgtaddressData.DataByteArray = value;
				list.Add(xgtaddressData);
			}
			OperateResult<XGT_MemoryType> operateResult = XGKFastEnet.AnalysisAddress(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				OperateResult<XGT_DataType, bool> dataTypeToAddress2 = XGKFastEnet.GetDataTypeToAddress(address);
				bool flag2 = !dataTypeToAddress2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<byte[]>(dataTypeToAddress2);
				}
				else
				{
					OperateResult<byte[]> operateResult2 = this.Write(dataTypeToAddress2.Content1, list, operateResult.Content, 0);
					bool flag3 = !operateResult2.IsSuccess;
					if (flag3)
					{
						result = operateResult2;
					}
					else
					{
						OperateResult<byte[]> operateResult3 = base.ReadBase(operateResult2.Content);
						bool flag4 = !operateResult3.IsSuccess;
						if (flag4)
						{
							result = operateResult3;
						}
						else
						{
							result = XGKCnet.ExtractActualData(operateResult3.Content, false);
						}
					}
				}
			}
			return result;
		}

		/// <summary>
		/// Read
		/// </summary>
		/// <param name="pDataType"></param>
		/// <param name="pAddress"></param>
		/// <param name="pMemtype"></param>
		/// <param name="pDataCount"></param>
		/// <returns></returns>
		// Token: 0x06000A49 RID: 2633 RVA: 0x0003D424 File Offset: 0x0003B624
		public OperateResult<byte[]> Read(XGT_DataType pDataType, List<XGTAddressData> pAddress, XGT_MemoryType pMemtype, int pDataCount = 0)
		{
			bool flag = pAddress.Count > 16;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = new OperateResult<byte[]>("You cannot read more than 16 pieces.");
			}
			else
			{
				try
				{
					byte[] value = this.CreateReadDataFormat(this.Station, XGT_Request_Func.Read, pDataType, pAddress, pMemtype, pDataCount);
					result = OperateResult.CreateSuccessResult<byte[]>(value);
				}
				catch (Exception ex)
				{
					result = new OperateResult<byte[]>("ERROR:" + ex.Message.ToString());
				}
			}
			return result;
		}

		/// <summary>
		/// Write
		/// </summary>
		/// <param name="pDataType"></param>
		/// <param name="pAddressList"></param>
		/// <param name="pMemtype"></param>
		/// <param name="pDataCount"></param>
		/// <returns></returns>
		// Token: 0x06000A4A RID: 2634 RVA: 0x0003D4A0 File Offset: 0x0003B6A0
		public OperateResult<byte[]> Write(XGT_DataType pDataType, List<XGTAddressData> pAddressList, XGT_MemoryType pMemtype, int pDataCount = 0)
		{
			OperateResult<byte[]> result;
			try
			{
				byte[] value = this.CreateWriteDataFormat(this.Station, XGT_Request_Func.Write, pDataType, pAddressList, pMemtype, pDataCount);
				result = OperateResult.CreateSuccessResult<byte[]>(value);
			}
			catch (Exception ex)
			{
				result = new OperateResult<byte[]>("ERROR:" + ex.Message.ToString());
			}
			return result;
		}

		// Token: 0x06000A4B RID: 2635 RVA: 0x0003D4FC File Offset: 0x0003B6FC
		private byte[] CreateReadDataFormat(byte station, XGT_Request_Func emFunc, XGT_DataType emDatatype, List<XGTAddressData> pAddressList, XGT_MemoryType emMemtype, int pDataCount)
		{
			List<XGTAddressData> list = new List<XGTAddressData>();
			foreach (XGTAddressData xgtaddressData in pAddressList)
			{
				string addressString = new XGKFastEnet().CreateValueName(emDatatype, emMemtype, xgtaddressData.Address);
				list.Add(new XGTAddressData
				{
					AddressString = addressString
				});
			}
			List<byte> list2 = new List<byte>();
			bool flag = XGT_DataType.Continue == emDatatype && XGT_Request_Func.Read == emFunc;
			if (flag)
			{
				list2.Add(5);
				list2.AddRange(SoftBasic.BuildAsciiBytesFrom(station));
				list2.Add(114);
				list2.Add(83);
				list2.Add(66);
				foreach (XGTAddressData xgtaddressData2 in list)
				{
					list2.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)xgtaddressData2.AddressString.Length));
					list2.AddRange(Encoding.ASCII.GetBytes(xgtaddressData2.AddressString));
					list2.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)pDataCount));
				}
				list2.Add(4);
				int num = 0;
				for (int i = 0; i < list2.Count; i++)
				{
					num += (int)list2[i];
				}
				list2.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)num));
			}
			else
			{
				list2.Add(5);
				list2.AddRange(SoftBasic.BuildAsciiBytesFrom(station));
				list2.Add(114);
				list2.Add(83);
				list2.Add(83);
				list2.Add(48);
				list2.Add(49);
				foreach (XGTAddressData xgtaddressData3 in list)
				{
					list2.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)xgtaddressData3.AddressString.Length));
					list2.AddRange(Encoding.ASCII.GetBytes(xgtaddressData3.AddressString));
				}
				list2.Add(4);
				int num2 = 0;
				for (int j = 0; j < list2.Count; j++)
				{
					num2 += (int)list2[j];
				}
				list2.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)num2));
			}
			return list2.ToArray();
		}

		// Token: 0x06000A4C RID: 2636 RVA: 0x0003D794 File Offset: 0x0003B994
		private byte[] CreateWriteDataFormat(byte station, XGT_Request_Func emFunc, XGT_DataType emDatatype, List<XGTAddressData> pAddressList, XGT_MemoryType emMemtype, int pDataCount)
		{
			List<XGTAddressData> list = new List<XGTAddressData>();
			foreach (XGTAddressData xgtaddressData in pAddressList)
			{
				string addressString = new XGKFastEnet().CreateValueName(emDatatype, emMemtype, xgtaddressData.Address);
				xgtaddressData.AddressString = addressString;
				list.Add(xgtaddressData);
			}
			List<byte> list2 = new List<byte>();
			bool flag = XGT_DataType.Continue == emDatatype && XGT_Request_Func.Write == emFunc;
			if (flag)
			{
				list2.Add(5);
				list2.AddRange(SoftBasic.BuildAsciiBytesFrom(station));
				list2.Add(119);
				list2.Add(83);
				list2.Add(66);
				foreach (XGTAddressData xgtaddressData2 in list)
				{
					list2.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)xgtaddressData2.AddressString.Length));
					list2.AddRange(Encoding.ASCII.GetBytes(xgtaddressData2.AddressString));
					list2.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)xgtaddressData2.AddressByteArray.Length));
					list2.AddRange(SoftBasic.BytesToAsciiBytes(xgtaddressData2.AddressByteArray));
				}
				list2.Add(4);
				int num = 0;
				for (int i = 0; i < list2.Count; i++)
				{
					num += (int)list2[i];
				}
				list2.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)num));
			}
			else
			{
				list2.Add(5);
				list2.AddRange(SoftBasic.BuildAsciiBytesFrom(station));
				list2.Add(119);
				list2.Add(83);
				list2.Add(83);
				list2.Add(48);
				list2.Add(49);
				foreach (XGTAddressData xgtaddressData3 in list)
				{
					list2.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)xgtaddressData3.AddressString.Length));
					list2.AddRange(Encoding.ASCII.GetBytes(xgtaddressData3.AddressString));
					list2.AddRange(SoftBasic.BytesToAsciiBytes(xgtaddressData3.AddressByteArray));
				}
				list2.Add(4);
				int num2 = 0;
				for (int j = 0; j < list2.Count; j++)
				{
					num2 += (int)list2[j];
				}
				list2.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)num2));
			}
			return list2.ToArray();
		}

		/// <summary>
		/// Extract actual data form plc response
		/// </summary>
		/// <param name="response">response data</param>
		/// <param name="isRead">read</param>
		/// <returns>result</returns>
		// Token: 0x06000A4D RID: 2637 RVA: 0x0003DA50 File Offset: 0x0003BC50
		public static OperateResult<byte[]> ExtractActualData(byte[] response, bool isRead)
		{
			OperateResult<byte[]> result;
			try
			{
				if (isRead)
				{
					bool flag = response[0] == 6;
					if (flag)
					{
						byte[] array = new byte[response.Length - 13];
						Array.Copy(response, 10, array, 0, array.Length);
						result = OperateResult.CreateSuccessResult<byte[]>(SoftBasic.AsciiBytesToBytes(array));
					}
					else
					{
						byte[] array2 = new byte[response.Length - 9];
						Array.Copy(response, 6, array2, 0, array2.Length);
						result = new OperateResult<byte[]>((int)BitConverter.ToUInt16(SoftBasic.AsciiBytesToBytes(array2), 0), "Data:" + SoftBasic.ByteToHexString(response));
					}
				}
				else
				{
					bool flag2 = response[0] == 6;
					if (flag2)
					{
						result = OperateResult.CreateSuccessResult<byte[]>(new byte[0]);
					}
					else
					{
						byte[] array3 = new byte[response.Length - 9];
						Array.Copy(response, 6, array3, 0, array3.Length);
						result = new OperateResult<byte[]>((int)BitConverter.ToUInt16(SoftBasic.AsciiBytesToBytes(array3), 0), "Data:" + SoftBasic.ByteToHexString(response));
					}
				}
			}
			catch (Exception ex)
			{
				result = new OperateResult<byte[]>(ex.Message);
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000A4E RID: 2638 RVA: 0x0003DB60 File Offset: 0x0003BD60
		public override string ToString()
		{
			return string.Format("XGkCnet[{0}:{1}]", base.PortName, base.BaudRate);
		}
	}
}
